#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

vector<string> State2ChessBoard(int state) {
  vector<string> chessboard = {
      "00000",
      "00000",
      "00000",
      "00000",
      "00000",
  };

  int pos = state % 25;

  chessboard[pos / 5][pos % 5] = '*';
  state /= 25;
  for (int i = 24; i >= 0; i--) {
    if (state & (1 << (24 - i))) {
      chessboard[i / 5][i % 5] = '1';
    }
  }
  return chessboard;
}

int ChessBoard2State(const vector<string> &chessboard) {
  int pos = 0, id = 0, state = 0;

  for (const string &row : chessboard) {
    for (const char &col : row) {
      state <<= 1;
      if (col == '*') {
        pos = id;
      } else if (col == '1') {
        state++;
      }
      id++;
    }
  }
  return state * 25 + pos;
}

int dir[8][2] = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2}};
vector<string> target_board = {"11111", "01111", "00*11", "00001", "00000"};
const int kMaxHalfDist = 8;

bool Check(int x, int y) {
  return x >= 0 && x < 5 && y >= 0 && y < 5;
}

unordered_map<int, int> BFS() {
  unordered_map<int, int> dist;
  queue<pair<int, int>> que;
  int target = ChessBoard2State(target_board);

  for (que.emplace(target, dist[target] = 0); !que.empty(); que.pop()) {
    int now_state = que.front().first;
    int now_dist = que.front().second;

    if (now_dist >= kMaxHalfDist) {
      break;
    }
    vector<string> chessboard = State2ChessBoard(now_state);
    int posx = now_state / 5 % 5, posy = now_state % 5;

    for (int d = 0; d < 8; d++) {
      int nx = posx + dir[d][0], ny = posy + dir[d][1];

      if (Check(nx, ny)) {
        swap(chessboard[posx][posy], chessboard[nx][ny]);
        int new_state = ChessBoard2State(chessboard);
        if (dist.count(new_state) == 0) {
          que.emplace(new_state, dist[new_state] = now_dist + 1);
        }
        swap(chessboard[posx][posy], chessboard[nx][ny]);
      }
    }
  }

  return dist;
}

int BFS2(int start, const unordered_map<int, int> &half_dist) {
  unordered_set<int> hash;
  queue<pair<int, int>> que;

  for (que.emplace(start, 0); !que.empty(); que.pop()) {
    int now_state = que.front().first;
    int now_dist = que.front().second;

    if (now_dist + 1 >= kMaxHalfDist) {
      break;
    }
    vector<string> chessboard = State2ChessBoard(now_state);
    int posx = now_state / 5 % 5, posy = now_state % 5;

    for (int d = 0; d < 8; d++) {
      int nx = posx + dir[d][0], ny = posy + dir[d][1];

      if (Check(nx, ny)) {
        swap(chessboard[posx][posy], chessboard[nx][ny]);
        int new_state = ChessBoard2State(chessboard);
        if (hash.count(new_state) == 0) {
          hash.insert(new_state);
          if (half_dist.count(new_state)) {
            return now_dist + 1 + kMaxHalfDist;
          }
          que.emplace(new_state, now_dist + 1);
        }
        swap(chessboard[posx][posy], chessboard[nx][ny]);
      }
    }
  }

  return -1;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;

  unordered_map<int, int> dist = BFS();

  for (cin >> T; T; T--) {
    vector<string> chessboard(5, "");

    for (int i = 0; i < 5; i++) {
      cin >> chessboard[i];
    }
    int now_state = ChessBoard2State(chessboard);

    if (dist.count(now_state)) {
      cout << dist[now_state] << "\n";
    } else {
      cout << BFS2(now_state, dist) << "\n";
    }
  }
  return 0;
}